package com.xidian.zhiancloud.controller.chemicalsManagement.dangerousPart;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.DangerCategory;
import com.xidian.zhiancloud.qo.chemicalsManagement.DangerCategoryQO;
import com.xidian.zhiancloud.service.chemicalsManagement.chemicalManager.dangerousPart.DangerCategoryService;
import com.xidian.zhiancloud.vo.chemicalsManagement.DangerCategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;

/**
 * 危化品管理 危化品类别设置
 *
 * @Author: Egoist
 * @Date: 2020/11/10 17:31
 */
@RestController
@RequestMapping("/DangerCategory")
public class DangerCategoryController extends BaseController {

    @Autowired
    private DangerCategoryService dangerCategoryService;

    /**
     * 增加危化品类型
     *
     * @param dangerCategory 危化品类别对象
     * @return 返回是否添加成功
     */
    @PostMapping("/add")
    public ResultDTO<Boolean> add(DangerCategory dangerCategory) {

        dangerCategory.setCreatorId(getSessionUserid());
        dangerCategory.setCompanyId(getSessionUserCompanyId());
        dangerCategory.setCreateTime(new Date(System.currentTimeMillis()));
        return dangerCategoryService.saveInfo(dangerCategory);

    }

    /**
     * 根据id获取危化品类别
     *
     * @param id 危化品类别对象的id
     * @return 危化品类别对象
     */
    @GetMapping("/getById")
    public ResultDTO<DangerCategory> getById(Long id) {

        return ResultDTO.successOf(dangerCategoryService.getById(id), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id删除危化品类别
     *
     * @param ids ids数组
     * @return 返回ResultDTO对象
     */
    @PostMapping("/deleteByIds")
    public ResultDTO<Boolean> deleteByIds(Long[] ids) {

        return ResultDTO.successOf(dangerCategoryService.removeByIds(Arrays.asList(ids)), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id修改危化品类别对象
     *
     * @param dangerCategory 危化品类别对象
     * @return 返回是否修改成功
     */
    @PostMapping("/patchById")
    public ResultDTO<Boolean> patchById(DangerCategory dangerCategory) {
        dangerCategory.setCreatorId(null);
        dangerCategory.setCompanyId(null);
        dangerCategory.setCreateTime(null);
        Long companyId = getSessionUserCompanyId();
        return dangerCategoryService.updateCheckNameById(dangerCategory, companyId);
    }

    /**
     * 列表查询
     * @param dangerCategoryQO 查询参数
     * @return 返回危化品类别列表
     */
    @GetMapping("/getListByQueryObject")
    public ResultDTO<IPage<DangerCategoryVO>> getListByQueryObject(DangerCategoryQO dangerCategoryQO) {
        if (dangerCategoryQO.getCurrent() == null || dangerCategoryQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        dangerCategoryQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(dangerCategoryService.getListByQueryObject(dangerCategoryQO), GeneralResponseEnums.SUCCESS);
    }

}
